package com.wwb.collect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.wwb.collect.domain.ErpWwbCollectProduct;
import com.wwb.collect.domain.ErpWwbCollectProductSku;
import com.wwb.collect.domain.bo.ErpWwbCollectProductBo;
import com.wwb.collect.domain.vo.ErpWwbCollectProductVo;
import com.wwb.collect.mapper.ErpWwbCollectProductMapper;
import com.wwb.collect.mapper.ErpWwbCollectProductSkuMapper;
import com.wwb.collect.service.IWwbCollectProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 微唯宝供应链采集商品
 * @Author: j.zh
 * @Date: 2024/6/6
 * @Version: 4.0.0
**/
@RequiredArgsConstructor
@Service
public class WwbCollectProductServiceImpl implements IWwbCollectProductService {

    @Resource
    ErpWwbCollectProductMapper wwbCollectProductMapper;
    @Resource
    ErpWwbCollectProductSkuMapper wwbCollectProductSkuMapper;

    @Override
    public TableDataInfo<ErpWwbCollectProductVo> queryPageList(ErpWwbCollectProductBo bo, PageQuery pageQuery) {
        // 获取已初步过滤过的最大ID
        Long maxId = wwbCollectProductMapper.selectMaxIdByOperateType();
        bo.setOperateType(0);
        bo.setCollectUserId(SecurityContextHolder.getUserId());
        LambdaQueryWrapper<ErpWwbCollectProduct> lqw = buildQueryWrapper(bo, maxId);
        Page<ErpWwbCollectProductVo> result = wwbCollectProductMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * @Description: 根据商品ID，获取商品所有的图片信息
     * @Author: j.zh
     * @Date: 2024/6/13
     * @param id
     * @return
     * @Version: 4.0.0
    **/
    @Override
    public Map<String, Object> queryImgUrlById(Long id) {
        ErpWwbCollectProduct product = wwbCollectProductMapper.selectById(id);
        if (product == null) {
            return null;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("main", product.getImageURLs().replaceAll("\\[", "")
                .replaceAll("\\]", "").replaceAll(",", ";")
                .replaceAll("，", ";"));
        result.put("white", product.getWhite() == null ? result.get("main").toString().split(";")[0] : product.getWhite()
                .replaceAll("\\[", "")
                .replaceAll("\\]", "").replaceAll(",", ";")
                .replaceAll("，", ";"));
        result.put("marketing", product.getMarketing() == null ? result.get("main").toString().split(";")[0] : product.getMarketing()
                .replaceAll("\\[", "")
                .replaceAll("\\]", "").replaceAll(",", ";")
                .replaceAll("，", ";"));
        result.put("detailImg", product.getDetailImg().replaceAll("\\[", "")
                .replaceAll("\\]", "").replaceAll(",", ";")
                .replaceAll("，", ";"));
        result.put("subject", product.getSubject());
        result.put("detail", product.getDetail());

        List<ErpWwbCollectProductSku> productSkuList = wwbCollectProductSkuMapper.selectList(new LambdaQueryWrapper<ErpWwbCollectProductSku>()
                .eq(ErpWwbCollectProductSku::getCollectProductId, id));
        Map<Long, String> skuResult = new HashMap<>();
        for (ErpWwbCollectProductSku sku : productSkuList) {
            if (sku.getSkuImage() == null || sku.getSkuImage().equals("")) {
                continue;
            }
            skuResult.put(sku.getId(), sku.getSkuImage().replaceAll("\\[", "")
                    .replaceAll("\\]", "").replaceAll(",", ";")
                    .replaceAll("，", ";"));
        }
        result.put("skuImgMap", skuResult);
        return result;
    }

    /**
     * @Description: 根据ID和左滑右滑指令，获取下一个商品图片集合
     * @Author: j.zh
     * @Date: 2024/6/14
     * @param map
     * @return
     * @Version: 4.0.0
    **/
    @Override
    public Map<String, Object> queryImgUrlByIdAndNext(Map<String, Object> map) {
        Integer isSave = Integer.parseInt(map.get("isSave").toString());
        Long id = Long.parseLong(map.get("id").toString());
        Integer next = Integer.parseInt(map.get("next").toString());
        String url = map.get("url").toString();
        ErpWwbCollectProduct product = new ErpWwbCollectProduct();
        if (isSave.intValue() == 1){
            // 保留该商品进跨境库
            product.setId(id);
            product.setOperateType(1);
            product.setWhite(url);
            wwbCollectProductMapper.updateById(product);
        }
        if (next > 0) {
            product = wwbCollectProductMapper.selectOne(new LambdaQueryWrapper<ErpWwbCollectProduct>()
                    .eq(ErpWwbCollectProduct::getOperateType, "0")
//                    .eq(ErpWwbCollectProduct::getCollectUserId, SecurityContextHolder.getUserId())
                    .gt(ErpWwbCollectProduct::getId, id)
                    .orderByAsc(ErpWwbCollectProduct::getId).last("limit 1"));
        } else {
            product = wwbCollectProductMapper.selectOne(new LambdaQueryWrapper<ErpWwbCollectProduct>()
                    .eq(ErpWwbCollectProduct::getOperateType, "0")
//                    .eq(ErpWwbCollectProduct::getCollectUserId, SecurityContextHolder.getUserId())
                    .lt(ErpWwbCollectProduct::getId, id)
                    .orderByDesc(ErpWwbCollectProduct::getId).last("limit 1"));
        }
        return queryImgUrlById(product.getId());
    }

    private LambdaQueryWrapper<ErpWwbCollectProduct> buildQueryWrapper(ErpWwbCollectProductBo bo, Long maxId) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpWwbCollectProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, ErpWwbCollectProduct::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getImageURLs()), ErpWwbCollectProduct::getImageURLs, bo.getImageURLs());
        lqw.eq(StringUtils.isNotBlank(bo.getSubject()), ErpWwbCollectProduct::getSubject, bo.getSubject());
        lqw.eq(StringUtils.isNotBlank(bo.getWhite()), ErpWwbCollectProduct::getWhite, bo.getWhite());
        lqw.eq(StringUtils.isNotBlank(bo.getMarketing()), ErpWwbCollectProduct::getMarketing, bo.getMarketing());
        lqw.eq(StringUtils.isNotBlank(bo.getMedia()), ErpWwbCollectProduct::getMedia, bo.getMedia());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectProductPrice()), ErpWwbCollectProduct::getCollectProductPrice, bo.getCollectProductPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getProductPrice()), ErpWwbCollectProduct::getProductPrice, bo.getProductPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getMobileDetail()), ErpWwbCollectProduct::getMobileDetail, bo.getMobileDetail());
        lqw.eq(bo.getPackageLength() != null, ErpWwbCollectProduct::getPackageLength, bo.getPackageLength());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageWidth()), ErpWwbCollectProduct::getPackageWidth, bo.getPackageWidth());
        lqw.eq(bo.getPackageHeight() != null, ErpWwbCollectProduct::getPackageHeight, bo.getPackageHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getGrossWeight()), ErpWwbCollectProduct::getGrossWeight, bo.getGrossWeight());
        lqw.eq(bo.getProductUnit() != null, ErpWwbCollectProduct::getProductUnit, bo.getProductUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomAttributes()), ErpWwbCollectProduct::getCustomAttributes, bo.getCustomAttributes());
        lqw.eq(StringUtils.isNotBlank(bo.getDetail()), ErpWwbCollectProduct::getDetail, bo.getDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailImg()), ErpWwbCollectProduct::getDetailImg, bo.getDetailImg());
        lqw.eq(bo.getSizechartId() != null, ErpWwbCollectProduct::getSizechartId, bo.getSizechartId());
        lqw.eq(StringUtils.isNotBlank(bo.getModeSizeChartList()), ErpWwbCollectProduct::getModeSizeChartList, bo.getModeSizeChartList());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectUrl()), ErpWwbCollectProduct::getCollectUrl, bo.getCollectUrl());
        lqw.eq(bo.getCollectUserId() != null, ErpWwbCollectProduct::getCollectUserId, bo.getCollectUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getOldDetailImg()), ErpWwbCollectProduct::getOldDetailImg, bo.getOldDetailImg());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpWwbCollectProduct::getRevision, bo.getRevision());
        lqw.eq(bo.getOperateType() != null, ErpWwbCollectProduct::getOperateType, bo.getOperateType());
        lqw.gt(maxId != null, ErpWwbCollectProduct::getId, maxId);
        return lqw;
    }

}
